
package siteanaliz;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileWriter;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.IOException;
import java.lang.StringBuffer;
import java.lang.Thread;
/**
 *
 * @author  Marie
 */
public class ParseEngine 
{
    private String SEngine;
    public ParseEngine(String type){ SEngine =type;}
    public ParseEngine(){SEngine="Google";}
    public String YandexConcurent(int count, String ResponseParse)
    {
        //ResponseParse.indexOf("b-serp-item__number");
        int item_count=0;
       
       // System.out.println(ResponseParse.substring(before_forger-1, before_forger));  //ResponseParse.indexOf("</b><a class="));
       // item_count=Integer.parseInt(ResponseParse.substring(before_forger-1, before_forger)); // получение позиции сайта
        int  before_forger=ResponseParse.indexOf(">"+Integer.toString(count)+"</b><a class=");
        int  after_forget=ResponseParse.indexOf("href=",  before_forger);
        int final_forget=ResponseParse.indexOf("onmousedown=",  after_forget);
        return ResponseParse.substring(after_forget+5, final_forget);
        
    }
    public String GoogleConcurent(int count, String ResponseParse)
    {
         int item_count=0;
         int  before_forget=0;
         for(int i=0; i<count; i++)
         {
            if(i>0) before_forget=ResponseParse.indexOf("<h3 class=\"r\"><a href=\"/url?q=", before_forget+30); // /url?q=http://
            else before_forget=ResponseParse.indexOf("<h3 class=\"r\"><a href=\"/url?q=");
            // <h3 class="r"><a href="/url?q=      
         }
         int middle_forget=ResponseParse.indexOf("&amp",before_forget);
         return ResponseParse.substring(before_forget+30, middle_forget);
         
    }
    
    public String RamblerConcurent(int count, String ResponseParse)
    {
         int item_count=0;
         int  before_forget=0;
         for(int i=1; i<=count; i++)
         {
            if(i>1) before_forget=ResponseParse.indexOf("<a target=\"_blank\" href=", before_forget+24); // /url?q=http://
            else before_forget=ResponseParse.indexOf("<a target=\"_blank\" href=");
            // <h3 class="r"><a href="/url?q=      
         }
         int middle_forget=ResponseParse.indexOf("onmouseup=",before_forget);
         return ResponseParse.substring(before_forget+24, middle_forget);
         
    }
    
    public String EngineIs(String EngPage)
    {
        if(EngPage.indexOf("www.google.com")!=-1) return "Google";
        else
        {
            if(EngPage.indexOf("www.yandex.ru")!=-1) return "Yandex";
            else 
            { 
                if(EngPage.indexOf("nova.rambler.ru")!=-1) return "Rambler";
                else   return "unknown";
            }
        }
    }
    
    
    public String Get3Concurents(int pos, Searcher ss, String tag)
    {
         String resultpos="";
          boolean capth=false;
         try
        {
            int j=1;
            String responsa=ss.makeRequest(tag);
            ResponseControl(responsa, 0);
            for(int i=1;i<pos;i++)
            {
                 if(SEngine.equals("Google"))
                {
                     if(i%100==0 )
                     {
                         ss.LoadingFrom(i);
                         responsa=ss.makeRequest(tag);
                         ResponseControl(responsa, i);
                     }
                      //System.out.println("сайт "+GoogleConcurent(i,responsa)+" поз "+i);
                     if(i>=pos-3&&i<pos) resultpos+="@"+Integer.toString(i)+"@"+"$U"+GoogleConcurent(i,responsa)+"$U";
                }
                else 
                {
                    if(SEngine.equals("Yandex"))
                    {
                        int top=i;
                         if(i%50==0 )
                        {
                         ss.LoadingFrom(i);
                         responsa=ss.makeRequest(tag);
                          ResponseControl(responsa, i);
                          j=1;
                        }
                        i=j;
                        if(i>=pos-3&&i<pos) resultpos+="@"+ Integer.toString(top)+"@"+"$U"+YandexConcurent(i,responsa)+"$U";
                        j++;
                        i=top;
                    }
                    else
                    {
                         if(SEngine.equals("Rambler"))
                         {  
                              int top=i;
                             if((i-1)%50==0 && (i-1)!=0 )// and bla bla bla
                            {
                             ss.LoadingFrom(i-1);
                             responsa=ss.makeRequest(tag);
                             ResponseControl(responsa, i);
                              j=1;
                             
                            }
                             i=j;
                            
                            if(i>=pos-3&&i<pos) resultpos+="@"+ Integer.toString(top)+"@"+"$U"+RamblerConcurent(i,responsa)+"$U";
                            j++;
                            i=top;
                         }
                    }
                }
            }
        }
         catch (Exception e) 
        {
            System.out.println("PositionExept-captcha");
             capth=true;
        }
         finally
        {
            if(resultpos.equals(""))
               return "unknown";
            else return resultpos;
        }
    }
    
    
    public String GetPosition(Searcher ss,int SearchingDeep, String URL_dom,String ktag)
    {
        String resultpos="";
        boolean capth=false;
        int j=1;
        try
        {
            String responsa=ss.makeRequest(ktag);
            ResponseControl(responsa, 0);
            for(int i=1;i<=SearchingDeep;i++)
            {
                if(SEngine.equals("Google"))
                {
                     if(i%100==0 )// and bla bla bla
                     {
                         ss.LoadingFrom(i);
                         responsa=ss.makeRequest(ktag);
                         ResponseControl(responsa, i);
                     }
                      //System.out.println("сайт "+GoogleConcurent(i,responsa)+" поз "+i);
                     if(DomainInclude(GoogleConcurent(i,responsa),URL_dom)) resultpos+="@"+Integer.toString(i)+"@"+"$U"+GoogleConcurent(i,responsa)+"$U";
                }
                else 
                {
                    if(SEngine.equals("Yandex"))
                    {
                        int top=i;
                         if(i%50==0 )// and bla bla bla
                        {
                         ss.LoadingFrom(i);
                         responsa=ss.makeRequest(ktag);
                          ResponseControl(responsa, i);
                          j=1;
                        }
                        i=j;
                        if(DomainInclude(YandexConcurent(i,responsa),URL_dom)) resultpos+="@"+ Integer.toString(top)+"@"+"$U"+YandexConcurent(i,responsa)+"$U";
                        j++;
                        i=top;
                    }
                    else
                    {
                         if(SEngine.equals("Rambler"))
                         {  
                              int top=i;
                             if((i-1)%50==0 && (i-1)!=0 )// and bla bla bla
                            {
                             ss.LoadingFrom(i-1);
                             responsa=ss.makeRequest(ktag);
                             ResponseControl(responsa, i);
                              j=1;
                             
                            }
                             i=j;
                             System.out.println("сайт "+RamblerConcurent(i,responsa)+" поз "+top);
                            if(DomainInclude(RamblerConcurent(i,responsa),URL_dom)) resultpos+="@"+ Integer.toString(top)+"@"+"$U"+RamblerConcurent(i,responsa)+"$U";
                            j++;
                            i=top;
                         }
                    }
                }
            }
        }
        catch (Exception e) 
        {
            System.out.println("PositionExept-captcha");
             capth=true;
        }
        finally
        {
            if(resultpos.equals(""))
               return "unknown";
            else return resultpos;
        }
    }
    
    public boolean DomainInclude(String URLS, String Domain)
    {
        boolean bool=true;
        String item=Domain;
        item="http://"+Domain;
        if(URLS.indexOf(item)==-1) bool=false;
        item="www."+Domain;
         if(URLS.indexOf(item)==-1 && bool==false) bool=false;
         else bool=true;
         item="https://"+Domain;
         if(URLS.indexOf(item)==-1 && bool==false) bool=false;
         else bool=true;
        //if(URLS.indexOf(Domain)==-1) return false;
        return bool;
        
    }
    
    public void ResponseControl(String sreq, int pos)
    {
         FileWriter writeFile = null;
         try {
                 File logFile = new File("searcher"+pos+".txt");
                 writeFile = new FileWriter(logFile);
                 writeFile.write(sreq+"\n");
             } 
         catch (IOException e) 
         {
                 System.out.println("ошибка работы с файлом");
         }
         finally {
               if(writeFile != null) {
                    try {
                    writeFile.close();
                    } 
                    catch (IOException e) {
                    System.out.println("ошибка работы с файлом");
                    }
               }
         }       
    }
    
    
}
